home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13458 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  55 lines

  1. Newsgroups: comp.lang.c++
  2. Path: news.athene.co.uk!not-for-mail
  3. From: "C.J. Scaife" <JOLTSWIFT@Athene.co.uk>
  4. Subject: object persistence & form()
  5. Message-ID: <315700AA.623E@Athene.co.uk>
  6. Date: Mon, 25 Mar 1996 20:23:06 +0000
  7. Organization: JoltSwift Ltd.
  8. X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
  9. MIME-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12.  
  13. Many initial versions of the standard C/C++ library included a
  14. function "char * form(const char * ...)", which was like sprintf(),
  15. but using it's own statically allocated buffer. This mostly seems
  16. to have disappeared with later releases of the respective C++
  17. compilers.
  18.  
  19. I suspect the reason is that form() was not a good idea:
  20. Extensive use in string handling soon results in unintentional and
  21. catastrophic re-entry. The following should make it obvious what I
  22. mean:
  23.  
  24.   cout << form("My birthday is %s", form("%d March", 11));
  25.  
  26. The obvious solution is to use a formatted constructor for an
  27. annonymous temporary instance of a string object class. In the
  28. following cs_sstr<120> is such a class. See
  29. http://www.Athene.co.uk/Joltswift/csact003.htm for explicit details.
  30.  
  31. The problem with annonymous variables in C++ is that they are
  32. destroyed on completion of the statement in which they are
  33. constructed, so this works:
  34.  
  35.   cout << cs_cstr<120>("One statement");
  36.  
  37. and this does not:
  38.  
  39.   char * Temp = cs_cstr<120>("Two statements");
  40.   cout << Temp;
  41.  
  42. In my humble opinion it would be much better if the usual object
  43. persistence rules were applicable to all automatically allocated
  44. objects be they annonymous or not.
  45.  
  46. Does anyone know why the standards committee decided to make an
  47. exception for annonymous variables, or how we might take advantage of
  48. it ?
  49.  
  50. -- 
  51. Written by Chris Scaife.
  52. For more details see my home page: 
  53. http://www.Athene.co.uk/Joltswift/P1.htm
  54.  
  55.